package cn.wildfire.chat.kit.conversation.wallet;

import android.text.TextUtils;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wildfire.chat.kit.AppService;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.entry.RedRecordResult;
import cn.wildfire.chat.kit.entry.RedResult;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfirechat.model.UserInfo;

public class PackgeRecordActivity extends WfcBaseActivity {
    @BindView(R2.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.tvTip)
    TextView tvTip;
    private RedRecordAdapter redRecordAdapter;
    private UserViewModel userModel;

    @Override
    protected int contentLayout() {
        return R.layout.activity_packge_record;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        if (!isDarkTheme()) {
            setTitleBackgroundResource(R.color.white, false);
        }
        userModel= ViewModelProviders.of(this).get(UserViewModel.class);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        redRecordAdapter = new RedRecordAdapter(userModel);
        mRecyclerView.setAdapter(redRecordAdapter);
        String redId = getIntent().getStringExtra("redId");
        receiveRecord(redId);

    }
    private void receiveRecord(String redId){
        if (TextUtils.isEmpty(redId)){
            return;
        }
        AppService.Instance().getRedRecord(redId,new AppService.RedRecordCallback() {
            @Override
            public void onUiSuccess(RedRecordResult data) {
                if (isFinishing()) {
                    return;
                }
                tvTitle.setText(data.getUid()+"的红包");
                tvContent.setText(data.getTitle());
                tvTip.setText(data.getFew()+"个红包，共"+data.getAmount()+"元");
                redRecordAdapter.setRedRecords(data.getRecords());
                List<String> ids = new ArrayList<>();
                for (RedRecordResult.RecordEntry entry : data.getRecords()) {
                    ids.add(entry.getRuid());
                }
                List<UserInfo> userInfos = userModel.getUserInfos(ids);
                for (int i = 0; i <redRecordAdapter.redRecords.size(); i++) {
                    redRecordAdapter.redRecords.get(i).setDisplayName(userInfos.get(i).displayName);
                }
                redRecordAdapter.notifyDataSetChanged();
            }

            @Override
            public void onUiFailure(int code, String msg) {
                if (isFinishing()) {
                    return;
                }
            }
        });
    }
    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}